package com.himiotech.core.project.service.impl;

import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.himiotech.core.framework.config.HimioConfig;
import com.himiotech.core.framework.config.server.ServerInfo;
import com.himiotech.core.framework.helper.constant.Constants;
import com.himiotech.core.framework.helper.utils.file.MimeTypeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wuwen文件管理
 */
@Slf4j
@Service
public class FileManagerServiceImpl {

    @Autowired
    private ServerInfo serverInfo;

    /**
     * 文件列表，用于文件管理查看的
     *
     * @param dir
     * @return
     */
    public List<Map<String, Object>> getFileListByDir(String dir) {
        String uploadPath = HimioConfig.getUploadPath();
        String path = uploadPath + dir;
        File[] ls = FileUtil.ls(path);
        List<Map<String, Object>> result = Arrays.stream(ls).map(file -> {
            Map<String, Object> temp = new HashMap<>(5);
            temp.put("name", file.getName());
            temp.put("updateTime", file.lastModified());
            boolean isDir = file.isDirectory();
            temp.put("isDir", isDir);
            if (!isDir) {
                String fileType = FileTypeUtil.getType(file);
                temp.put("type", fileType);
                String url = serverInfo.getUrl() + Constants.UPLOAD_FILE_PREFIX + "/upload" + dir + "/" + file.getName();
                temp.put("url", url);
                if (Arrays.stream(MimeTypeUtils.IMAGE_EXTENSION).collect(Collectors.toList()).contains(fileType)) {
                    temp.put("hasSm", true);
                    temp.put("smUrl", url);
                }
            } else {
                temp.put("type", "dir");
            }
            return temp;
        }).collect(Collectors.toList());
        return result;
    }

    /**
     * 删除文件
     *
     * @param absPath 文件绝对路径
     * @return
     */
    public boolean removeFile(String absPath) {
        if (StrUtil.isNotEmpty(absPath)) {
            try {
                boolean exist = FileUtil.exist(absPath);
                if (exist) {
                    if (FileUtil.isFile(absPath)) {
                        return FileUtil.del(absPath);
                    }
                }
                log.info("目标文件：{} 不存在，删除失败", absPath);
                return false;
            } catch (Exception e) {
                log.error("删除文件失败，{}", e.getMessage());
                return false;
            }
        }
        return false;
    }

    /**
     * 添加文件夹
     *
     * @param dir
     * @param name
     */
    public void addFolder(String dir, String name) {
        String foldPath = HimioConfig.getUploadPath() + dir + "/" + name;
        FileUtil.mkdir(foldPath);
    }
}
